﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ArchitectingInnovation.PRISMvvM.Core.Extensions
{
    public static class UserControlExtensions
    {
        public static DependencyProperty BindingScopeProperty = DependencyProperty.Register("BindingScope",
                                                                                            typeof (BindingScope),
                                                                                            typeof (FrameworkElement),
                                                                                            new PropertyMetadata(null,
                                                                                                                 OnBindingScopeChanged));

        public static void OnBindingScopeChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var elt = sender as FrameworkElement;
            if (elt == null) return;

            var scope = elt.GetValue(BindingScopeProperty) as BindingScope;
            if (scope == null)
                return;
            scope.Bind(elt);
        }

        public static DependencyProperty EventToCommandProperty = DependencyProperty.Register("EventToCommand",
                                                                                              typeof (
                                                                                                  EventToCommandConverter
                                                                                                  ),
                                                                                              typeof (FrameworkElement),
                                                                                              new PropertyMetadata(
                                                                                                  null,
                                                                                                  OnEventToCommandChanged));


        public static void OnEventToCommandChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var elt = sender as FrameworkElement;
            if (elt == null) return;

            var scope = elt.GetValue(EventToCommandProperty) as BindingScope;
            if (scope == null)
                return;
            var dataContext = elt.DataContext;
            var eventToCommandConverter = elt.GetValue(EventToCommandProperty) as EventToCommandConverter;
            if (eventToCommandConverter == null) return;
            eventToCommandConverter.Bind(dataContext, elt);
        }


    }
}
